import {Component} from "@angular/core";
import {Storage} from "@ionic/storage";

import {NavController, ToastController} from "ionic-angular";
import {UserService} from "../../service/user/user.service";
import {LoginPage} from "../login/login";
import {SmsService} from "../../service/sms/sms.service";


@Component({
  selector: 'page-register',
  templateUrl: 'register.html',
  providers: [UserService, SmsService]
})
export class RegisterPage {


  private registerData: any = {
    nickname: null,
    cellphone: null,
    password: null,
    invitationCode: null,
    verificationCode: null,
    sendVerificationCodeMessage:'获取验证码'
  };
  private smsDisable: boolean = false;

  constructor(private navCtrl: NavController,
              private userService: UserService,
              private toast: ToastController,
              private smsService: SmsService,
              private  storage: Storage) {
  }

  toLogin() {
    this.navCtrl.push(LoginPage);
  }
  verificationPhone(cellphone: string) {
    return /^1([34578])\d{9}$/.test(cellphone);
    // return this.smsDisable;
  }
  sendVerificationCode() {
    if (!this.verificationPhone(this.registerData.cellphone)) {
      this.toast.create({
        message: '手机号格式不正确!',
        duration: 2000,
        position: 'top'
      }).present();
      return;
    }
    this.smsDisable = true;
    this.registerData.sendVerificationCodeMessage = "正在获取中...";


    this.smsService.sendVerificationCode({cellphone: this.registerData.cellphone}).subscribe(res => {
      if (res.status === "SUCCESS") {
        this.toast.create({
          message: "验证码发送成功",
          duration: 2000,
          position: 'top'
        }).present();
        let time = 60;
        const timeMessage = setInterval(() => {
          if (time <= 1) {
            clearTimeout(timeMessage);
            this.registerData.sendVerificationCodeMessage = "重新获取";
            this.smsDisable = false;
            return;
          }
          this.registerData.sendVerificationCodeMessage = "重新获取(" + time + ")";
          time--;
        }, 1000);
      } else {
        this.toast.create({
          message: res.message,
          duration: 2000,
          position: 'top'
        }).present();
        this.registerData.sendVerificationCodeMessage = "重新获取";
        this.smsDisable = false;
      }
    });
  }

  registerBtnEnable() {
    return this.userService.registerDataValidate(this.registerData).status
  }

  registerClick() {
    let checkedResult = this.userService.registerDataValidate(this.registerData);
    if (!checkedResult.status) {
      this.toast.create({
        message: checkedResult.msg,
        duration: 2000,
        position: 'top'
      }).present();
      return;
    }

    this.userService.register(this.registerData).subscribe(res => {
      if (res.status === "SUCCESS") {
        this.storage.set("user", res.data.userEntity);
        this.storage.set("oauthToken", res.data.oauthToken);
        this.navCtrl.pop()
      } else {
        this.toast.create({
          message: res.message,
          duration: 2000,
          position: 'top'
        }).present();
      }
    },error=>{
      this.toast.create({
        message: error.message,
        duration: 2000,
        position: 'top'
      }).present();
    });
  }
}
